﻿using System;
using System.Data;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyTuyenSinh.BussinessLayer;
using QuanLyTuyenSinh.Properties;

namespace QuanLyTuyenSinh.PresentationLayer
{
    public partial class frmZone : Office2007Form
    {
        private int ma;

        public frmZone()
        {
            InitializeComponent();
        }
        private void HideControl(bool hien)
        {
            BtnAdd.Visible = hien;
            BtnSave.Visible = hien;
            BtnDelete.Visible = hien;
            BtnAdd.Enabled = true;
            BtnDelete.Enabled = true;
        }
        private void FrmKhuvucLoad(object sender, EventArgs e)
        {
            try
            {
                DataTable table = Operator.Operator.GetListZone();
                if (table.Rows.Count > 0)
                {
                    dataGridKhuvuc.DataSource = table;
                }
                HideControl(false);
                BtnAdd.Visible = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void BtnAddClick(object sender, EventArgs e)
        {
            txtMaKV.Clear();
            txtTenKV.Clear();
           
            HideControl(true);
            ma = 0;
            BtnDelete.Visible = false;
        }

        private void BtnSaveClick(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtMaKV.Text.Trim()))
                {
                    MessageBox.Show("Chưa nhập: Mã khu vực");
                    return;
                }
                if (string.IsNullOrEmpty(txtDiemKV.Text.Trim()))
                {
                    MessageBox.Show("Chưa nhập: Điểm khu vực");
                    return;
                }
                var ethnic = new Zone();
                ethnic.ZoneDisplay = txtMaKV.Text.Trim();
                ethnic.ZoneName = txtTenKV.Text.Trim();
                ethnic.ZoneNumber = decimal.Parse(txtDiemKV.Text.Trim());
                DataTable listEthnic = Operator.Operator.GetListZoneById(ma);
                if (listEthnic.Rows.Count > 0)
                {
                    ethnic.Id = ma;
                    int m = ethnic.Update(ethnic);
                    if (m > 0)
                    {
                        FrmKhuvucLoad(sender, e);
                    }
                }
                else
                {
                    int m = ethnic.Insert(ethnic);
                    if (m > 0)
                    {
                        FrmKhuvucLoad(sender, e);
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }
        }

        private void BtnDeleteClick(object sender, EventArgs e)
        {
            try
            {
                DialogResult DRL;
                DRL = MessageBox.Show(Resources.Delete, Resources.Header, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (DRL == DialogResult.No)
                {
                    return;
                }
                else
                {
                    var ethnic = new Zone();
                    ethnic.Id = ma;
                    int m = ethnic.Delete(ethnic);
                    if (m > 0)
                    {
                        FrmKhuvucLoad(sender, e);
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }
        }

        private void DataGridKhuvucCellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex;
            if (i < 0) return;
            else
            {
                ma = int.Parse(dataGridKhuvuc[0, i].Value.ToString());
                txtMaKV.Text = dataGridKhuvuc[1, i].Value.ToString();
                txtTenKV.Text = dataGridKhuvuc[2, i].Value.ToString();
                txtDiemKV.Text = dataGridKhuvuc[3, i].Value.ToString();
                HideControl(true);
            }
        }

        private void FrmZoneKeyDown(object sender, KeyEventArgs e)
        {
            if ((e.Control) && (e.KeyCode == Keys.S) && BtnSave.Visible)
            {
                BtnSaveClick(sender, e);
            }
            else if (e.Control && e.KeyCode == Keys.N)
            {
                BtnAddClick(sender, e);
            }
            else if ((e.KeyCode == Keys.Delete) && (BtnDelete.Visible))
            {
                BtnDeleteClick(sender, e);
            }
        }
    }
}